package com.jxnu.project_z_android.ui.main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.view.View;

import com.jxnu.project_z_android.R;
import com.jxnu.project_z_android.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding activityMainBinding;
    private MainViewModel mainActivityViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mainActivityViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mainActivityViewModel.setActivity(this);
        initData();
        initEvent();
    }


    private void initData() {
        mainActivityViewModel.getStringData().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                activityMainBinding.txHello.setText(s);
            }
        });
    }

    private void initEvent() {
        activityMainBinding.btClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivityViewModel.updateText();
            }
        });
    }

}
